package com.cn.xiaonuo.common.utils;

import java.text.SimpleDateFormat;
import java.util.*;

public class DateTimeUtils {

    //根据日期查询本周日期范围
    public static  String getTimeInterval(Date date) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        // 判断要计算的日期是否是周日，如果是则减一天计算周六的，否则会出问题，计算到下一周去了
        int dayWeek = cal.get(Calendar.DAY_OF_WEEK);// 获得当前日期是一个星期的第几天
        if (1 == dayWeek) {
            cal.add(Calendar.DAY_OF_MONTH, -1);
        }
        // System.out.println("要计算日期为:" + sdf.format(cal.getTime())); // 输出要计算日期
        // 设置一个星期的第一天，按中国的习惯一个星期的第一天是星期一
        cal.setFirstDayOfWeek(Calendar.MONDAY);
        // 获得当前日期是一个星期的第几天
        int day = cal.get(Calendar.DAY_OF_WEEK);
        // 根据日历的规则，给当前日期减去星期几与一个星期第一天的差值
        cal.add(Calendar.DATE, cal.getFirstDayOfWeek() - day);
        String imptimeBegin = sdf.format(cal.getTime());
        //System.out.println("所在周星期一的日期：" + imptimeBegin);
        cal.add(Calendar.DATE, 6);
        String imptimeEnd = sdf.format(cal.getTime());
        //System.out.println("所在周星期日的日期：" + imptimeEnd);
        return imptimeBegin + "," + imptimeEnd;
    }

    public static String getLastTimeInterval() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Calendar calendar1 = Calendar.getInstance();
        Calendar calendar2 = Calendar.getInstance();
        int dayOfWeek = calendar1.get(Calendar.DAY_OF_WEEK) - 1;
        int offset1 = 1 - dayOfWeek;
        int offset2 = 7 - dayOfWeek;
        calendar1.add(Calendar.DATE, offset1 - 7);
        calendar2.add(Calendar.DATE, offset2 - 7);
        String lastBeginDate = sdf.format(calendar1.getTime());
        String lastEndDate = sdf.format(calendar2.getTime());
        return lastBeginDate + "," + lastEndDate;
    }

    //传入开始与结束时间
    public static Map<String, String> conversionTime(Long beginTime, Long endTime) {
        Map<String, String> map = new HashMap<>(16);
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        try {
            String begin = format.format(new Date(beginTime)) + " 00:00:00";
            String end = format.format(new Date(endTime)) + " 23:59:59";
            map.put("begin", begin);
            map.put("end", end);
        } catch (Exception e) {
            System.out.print("****************时间转换错误*******************");
            e.printStackTrace();
        }
        return map;
    }

    //获得昨日起始时间
    public static String getYDayStart(){
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.HOUR_OF_DAY,-24);

        String yDayStartTime = new SimpleDateFormat("yyyy-MM-dd").format(calendar.getTime())+" 00:00:00";
        return yDayStartTime;
    }

    //获得昨日结束时间
    public static String getYDayEnd(){
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.HOUR_OF_DAY,-24);

        String yDayEndTime = new SimpleDateFormat("yyyy-MM-dd").format(calendar.getTime())+" 23:59:59";
        return yDayEndTime;
    }

    public static Date getTimeAddSecond(Date  date  ,int second) {
        Calendar calendar = new GregorianCalendar();
        calendar.setTime(date);
        calendar.add(Calendar.SECOND, second);
        return calendar.getTime();
    }

}
